//
//  XFDRegisterController.m
//  XFDLogin_Example
//
//  Created by 韩冰 on 2017/11/6.
//  Copyright © 2017年 GL-ZHU. All rights reserved.
//

#import "XFDRegisterController.h"
#import "XFDRegisterOneView.h"
#import "XFDRegisterTwoView.h"
#import "XFDRegisterThreeView.h"
#import <XFDCommon/HBCommon.h>
#import <BlocksKit/UIControl+BlocksKit.h>
#import <Masonry/Masonry.h>
#import "XFDCommitSuccessController.h"
#import "XFDLoginAPI.h"
#import "XFDLogInNetAPI.h"
#import <XFDCommon/NSDictionary+HBSafely.h>
#import <XFDCommon/XFDUserManage.h>

@interface XFDRegisterController ()
@property (nonatomic, strong)UIScrollView * scrollView;
@property (nonatomic, strong)UIButton *logInButton;
@property (nonatomic, strong)XFDRegisterOneView *oneView;
@property (nonatomic, strong)XFDRegisterTwoView *twoView;
@property (nonatomic, strong)XFDRegisterThreeView *threeView;
@property (nonatomic, copy)NSString *phone;
@property (nonatomic, copy)NSString *code;
@end

@implementation XFDRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createSubViews];
    self.title = @"注册1/3";
    [self wr_setNavBarShadowImageHidden:YES];
    WEAKSELF
    self.oneView.nextBlock = ^(NSString *text){
        STRONGSELF
        strongSelf.phone = text;
        [strongSelf getCodeWithPhone:text withFirst:YES];
    };
    self.twoView.nextBlock = ^(NSString *text) {
        STRONGSELF
        strongSelf.code = text;
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValueSafely:text forKey:@"smsCode"];
        [dic setValueSafely:strongSelf.phone forKey:@"mobilephone"];
        [dic setValueSafely:@"REGISTER" forKey:@"type"];
        [strongSelf checkCode:dic];
    };
    self.twoView.getCodeBlock = ^{
        STRONGSELF
       
        [strongSelf getCodeWithPhone:strongSelf.phone withFirst:NO];
    };
    self.threeView.commitBlock = ^(NSMutableDictionary *dict) {
      STRONGSELF
        [strongSelf setUserInfo:dict];
    };
}

- (void)getCodeWithPhone:(NSString*)phone withFirst:(BOOL)isFirst{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValueSafely:phone forKey:@"mobilephone"];
    [dic setValueSafely:@"REGISTER" forKey:@"type"];
    WEAKSELF
    [XFDLogInNetAPI getVerificationcode:dic WithSuccessBlock:^(id response) {
        STRONGSELF
        if (isFirst) {
            [strongSelf.oneView inputViewResignFirstResponder];
            [UIView animateWithDuration:0.3 animations:^{
                strongSelf.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH , 0);
                [strongSelf.twoView inputViewBecomeFirstResponder];
                [strongSelf.twoView  openCodeBtnCountdown];
                self.navigationItem.title = @"注册2/3";
            }];
        }else{
            [strongSelf.twoView  openCodeBtnCountdown];
            [strongSelf.twoView inputViewBecomeFirstResponder];
        }
    } WithFailurBlock:^(NSError *error, NSString *tips) {
        
    }];
}

- (void)checkCode:(NSMutableDictionary *)dic{
    WEAKSELF
    [XFDLogInNetAPI checksmscode:dic WithSuccessBlock:^(id response) {
        STRONGSELF
        [strongSelf.twoView inputViewResignFirstResponder];
        [UIView animateWithDuration:0.3 animations:^{
            strongSelf.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH*2 , 0);
            self.navigationItem.title = @"注册3/3";
        }];
        [strongSelf.threeView inputViewBecomeFirstResponder];
    } WithFailurBlock:^(NSError *error, NSString *tips) {
        
    }];
}

- (void)setUserInfo:(NSMutableDictionary *)dict{
    
    WEAKSELF
    if ([XFDUserManage getUserType ] == XFDUserTypeFlyer){
        [dict setValueSafely:@"FLYER" forKey:@"roleCode"];
    } else {
        [dict setValueSafely:@"ADMIN" forKey:@"roleCode"];
    }
    [dict setValueSafely:self.phone forKey:@"mobilephone"];
    [dict setValueSafely:self.code forKey:@"smsCode"];
    [XFDLogInNetAPI setRegisterInfo:dict WithSuccessBlock:^(id response) {
        STRONGSELF
        if ([XFDUserManage getUserType ] == XFDUserTypeFlyer) {
            [strongSelf.navigationController popToRootViewControllerAnimated:YES];
        }else{
            [strongSelf.threeView inputViewResignFirstResponder];
            [strongSelf.navigationController pushViewController:[XFDLoginAPI share].commitCtr animated:YES];
        }
    } WithFailurBlock:^(NSError *error, NSString *tips) {
        
    }];
    
}

- (void)dealloc{
    
}


- (void)createSubViews{
    [self.view addSubview:self.logInButton];
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.oneView];
    [self.scrollView addSubview:self.twoView];
    [self.scrollView addSubview:self.threeView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(20) ;
        make.height.mas_offset(SCREEN_HEIGHT - 100);
        make.right.equalTo(self.view.mas_right);
        make.left.equalTo(self.view.mas_left);
    }];
    [self.logInButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollView.mas_bottom);
        make.bottom.equalTo(self.view.mas_bottom);
        make.left.equalTo(self.view.mas_left).with.offset(40);
        make.width.mas_offset(80);
    }];
    self.oneView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 100);
    self.threeView.frame = CGRectMake(SCREEN_WIDTH *2, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 100);
    self.twoView.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 100);
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView= [[UIScrollView alloc]init];
        _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 3, SCREEN_HEIGHT- 50);
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.scrollEnabled = NO;
    }
    return _scrollView;
}

- (XFDRegisterOneView *)oneView{
    if (!_oneView) {
        _oneView = [[XFDRegisterOneView alloc]init];
    }
    return _oneView;
}
- (XFDRegisterTwoView *)twoView{
    if (!_twoView) {
        _twoView = [[XFDRegisterTwoView alloc]init];
    }
    return _twoView;
}
- (XFDRegisterThreeView *)threeView{
    if (!_threeView) {
        _threeView = [[XFDRegisterThreeView alloc]init];
    }
    return _threeView;
}
- (UIButton *)logInButton{
    if (!_logInButton) {
        _logInButton  = [UIButton buttonWithType:UIButtonTypeCustom];
        _logInButton.titleLabel.font = [UIFont systemFontOfSize:12];
        _logInButton.titleLabel.textAlignment = NSTextAlignmentRight;
        [_logInButton setTitle:@"已有账户" forState:UIControlStateNormal];
        [_logInButton setTitleColor:[UIColor HB_ColorWithHexString:@"#2fa1f3"] forState:UIControlStateNormal];
        WEAKSELF
        [_logInButton bk_addEventHandler:^(id sender) {
            STRONGSELF
            [strongSelf.navigationController popViewControllerAnimated:YES];
        } forControlEvents:UIControlEventTouchUpInside];
    }
    return _logInButton;
}
@end
